import com.mysql.cj.Session;
import gz.Music;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String name=request.getParameter("name");
        String pass=request.getParameter("pass");
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url="jdbc:mysql:///student?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT&allowPublicKeyRetrieval=true";
            String user = "root";
            String pwd = "123456";
            conn = DriverManager.getConnection(url, user, pwd);
            String sql = "select * from admin";
            pstm = conn.prepareStatement(sql);
            rs = pstm.executeQuery();
            while (rs.next()){
                if (name.equals(rs.getString("name"))&&pass.equals(rs.getString("password"))) {
                    System.out.println(request.getParameter("checkbox"));
                    HttpSession session=request.getSession();
                    if (request.getParameter("checkbox")!=null){
                        session.setAttribute("name",name);
                        session.setAttribute("pass",pass);
                        session.setAttribute("checkbox","1");
                    }else{
                        Enumeration<String> attributeNames1 = session.getAttributeNames();
                        while (attributeNames1.hasMoreElements()){
                            session.removeAttribute(attributeNames1.nextElement());
                        }
                    }
                    request.setAttribute("name",name);
                    request.getRequestDispatcher("SqlServlet").forward(request,response);
                }
            }
            request.setAttribute("login", "用户名或密码错误!");
            request.getRequestDispatcher("login.jsp").forward(request,response);
            if (rs!=null){
                rs.close();
            }if (pstm!=null){
                pstm.close();
            }if (conn!=null){
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
